package view;


import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.text.TextUtils;
import android.widget.TextView;

import com.example.xuegengwang.xuegengwang.R;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.IMarker;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.MarkerView;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.github.mikephil.charting.formatter.IValueFormatter;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.utils.MPPointF;
import com.github.mikephil.charting.utils.ViewPortHandler;

import java.util.ArrayList;

import utils.UnitUtils;
import willevaluate.bean.LineChartBean;
import willevaluate.bean.WillCollegeDetailBean;

/**
 * Created by Administrator on 2017/5/6.
 * type是判断是分数还是排名
 */

public class MyLineChart {
    public static final int SCORE = 1;
    public static final int RANK = 0;

    public MyLineChart(Context context, LineChart lineChart, final ArrayList<LineChartBean> arrayList, int type) {
        lineChart.clear();
        // y轴的数据
        ArrayList<Entry> yValues = new ArrayList<>();
        for (int i = 0; i < arrayList.size(); i++) {
            if (arrayList.get(i).getNum() != null) {
                if (!TextUtils.isEmpty(arrayList.get(i).getNum())) {
                    yValues.add(new Entry(i, Float.parseFloat(arrayList.get(i).getNum())));
                }
            }
        }
        LineDataSet lineDataSet = new LineDataSet(yValues, null);
        LineData lineData = new LineData();
        lineData.addDataSet(lineDataSet);
        lineChart.setDrawBorders(false);
        lineDataSet.setLineWidth(1f); // 线宽
        lineDataSet.setCircleSize(5f);// 显示的圆形大小
        lineDataSet.calcMinMax();
        int common_color;
        if (type == SCORE) {
            common_color = context.getResources().getColor(R.color.common_color);
        } else {
            common_color = context.getResources().getColor(R.color.green);
        }
        lineDataSet.setCircleColorHole(common_color);
        lineDataSet.setColor(common_color);// 显示颜色
        lineDataSet.setCircleColor(common_color);// 圆形的颜色
        lineDataSet.setHighlightEnabled(true);//是否打开高亮线
        lineDataSet.setHighLightColor(Color.TRANSPARENT); // 高亮的线的颜色
        lineDataSet.setDrawValues(true);//设置每个点的值;
        lineDataSet.setValueFormatter(new IValueFormatter() {
            @Override
            public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
                return String.valueOf((int) value);
            }
        });
        lineDataSet.setValueTextColor(common_color);
        XAxis xAxis = lineChart.getXAxis();//设置x轴属性
        xAxis.setEnabled(true);
        xAxis.setDrawAxisLine(true);   //是否绘制坐标轴的线，即含有坐标的那条线，默认是true
        xAxis.setDrawGridLines(true); //是否显示X坐标轴上的刻度竖线，默认是true
        float[] floats = new float[]{5, 2};
        DashPathEffect dashPathEffect = new DashPathEffect(floats, 1);
        xAxis.setGridDashedLine(dashPathEffect);
        xAxis.setDrawLabels(true);
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setTextSize(UnitUtils.dip2px(3)); //X轴上的刻度的字的大小 单位dp
        xAxis.setTextColor(context.getResources().getColor(R.color.text_color));
        xAxis.setGranularityEnabled(false);
        if (arrayList.size() > 1) {
            xAxis.setLabelCount(arrayList.size(), true);
            xAxis.setValueFormatter(new IAxisValueFormatter() {
                @Override
                public String getFormattedValue(float value, AxisBase axis) {
                    if (value < arrayList.size()) {
                        return arrayList.get((int) value).getYear() + "年";
                    } else {
                        return null;
                    }
                }
            });
        }
        // enable scaling and dragging
        lineChart.setDragEnabled(false);// 是否可以拖拽
        lineChart.setScaleXEnabled(false);
        lineChart.setAutoScaleMinMaxEnabled(false);
        lineChart.setScaleYEnabled(false);
        // if disabled, scaling can be done on x- and y-axis separatey
        lineChart.setPinchZoom(false);//
        YAxis leftAxis = lineChart.getAxisLeft();//设置左边的y轴
        leftAxis.setEnabled(false);
        leftAxis.setDrawZeroLine(false);
        leftAxis.setSpaceTop(20f);//设置最大值与view的顶部的距离
        YAxis rightAxis = lineChart.getAxisRight();//设置右边的y轴
        rightAxis.setEnabled(false);
        rightAxis.setDrawZeroLine(false);
        rightAxis.setSpaceTop(20f);//设置最大值与view的顶部的距离
        lineChart.setData(lineData); // 设置数据
        lineChart.setSelected(true);
        lineChart.setDescription(null);
        Legend mLegend = lineChart.getLegend(); // 设置比例图标示，就是那个一组y的value的
        mLegend.setEnabled(false);//关闭左下角描述
    }
}
